﻿/*
 *  Date:2015/8/4
 *  Author: huang hao
 */
using System;
using System.Collections;

namespace IceCS.PrimesGenerator
{
    public class Primes:IEnumerable
    {
        private long min;
        private long max;

        public Primes()
        {

        }

        public Primes(long miniNum, long maxiNum)
        {
            if (miniNum < 2)
            {
                min = 2;
            }
            else
            {
                min = miniNum;
            }
            max = maxiNum;
        }

        public IEnumerator GetEnumerator()
        {
            for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
            {
                bool isPrime = true;
                for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
                {
                    long remainderAfterDivision = possiblePrime % possibleFactor;
                    if (remainderAfterDivision == 0)
                    {
                        isPrime = false;
                        break; ;
                    }
                }
                if (isPrime)
                {
                    yield return possiblePrime;
                }
            }
        }
    }
}